package cn.congine.wizarpos.mall.tenpay;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.StringUtils;

import cn.congine.wizarpos.mall.tenpay.util.XMLUtil;

public class QQPrePayRequest implements Serializable {
	private static final long serialVersionUID = 1L;
	
	/** 版本号 默认是1.0，目前版本取值应为2.0 */
	private String ver = "2.0";
	/** 字符集 1:UTF-8 2:GB2312 默认1 */
	private String charset = "1";
	/** 银行类型:填 0 */
	private String bank_type = "0";
	/** 商品描述 32字符以内 */
	private String desc;
	/** 用户(买方)的 QQ 号码。若商户取不到则此字段可不传 */
	private String purchaser_id;
	/** 描述支付渠道 1 手 Q支付  */
	private String pay_channel = "1";
	/** 商户号 由手Q支付系统统一分配的10位正整数(120XXXXXXX) */
	private String bargainor_id;
	/** 订单号 商户系统内部的订单号,32个字符内 */
	private String sp_billno;
	/** 总金额 以分为单位 */
	private Integer total_fee;
	/** 币种 现金支付币种,目前只支持人民币 1:人民币 */
	private Integer fee_type = 1;
	/** 通知地址 接收手Q支付通知的URL，需给绝对路径,255字符内 */
	private String notify_url;
	/** 附加信息 可做拓展参数,255字符内 非必填 */
	private String attach;
	/** 订单生成时间 格式为yyyymmddhhmmss 时间取自服务器 非必填 */
	private String time_start;
	/** 订单超时时间 非必填 */
	private String time_expire;
	/** MD5签名结果 通过签名算法得到 */
	private String sign;
	/** 渠道商支付id */
	private String sign_sp_id;
	
	public String getVer() {
		return ver;
	}
	public void setVer(String ver) {
		this.ver = ver;
	}
	public String getCharset() {
		return charset;
	}
	public void setCharset(String charset) {
		this.charset = charset;
	}
	public String getBank_type() {
		return bank_type;
	}
	public void setBank_type(String bank_type) {
		this.bank_type = bank_type;
	}
	public String getDesc() {
		return desc;
	}
	public void setDesc(String desc) {
		this.desc = desc;
	}
	public String getPurchaser_id() {
		return purchaser_id;
	}
	public void setPurchaser_id(String purchaser_id) {
		this.purchaser_id = purchaser_id;
	}
	public String getPay_channel() {
		return pay_channel;
	}
	public void setPay_channel(String pay_channel) {
		this.pay_channel = pay_channel;
	}
	public String getBargainor_id() {
		return bargainor_id;
	}
	public void setBargainor_id(String bargainor_id) {
		this.bargainor_id = bargainor_id;
	}
	public String getSp_billno() {
		return sp_billno;
	}
	public void setSp_billno(String sp_billno) {
		this.sp_billno = sp_billno;
	}
	public Integer getTotal_fee() {
		return total_fee;
	}
	public void setTotal_fee(Integer total_fee) {
		this.total_fee = total_fee;
	}
	public Integer getFee_type() {
		return fee_type;
	}
	public void setFee_type(Integer fee_type) {
		this.fee_type = fee_type;
	}
	public String getNotify_url() {
		return notify_url;
	}
	public void setNotify_url(String notify_url) {
		this.notify_url = notify_url;
	}
	public String getAttach() {
		return attach;
	}
	public void setAttach(String attach) {
		this.attach = attach;
	}
	public String getTime_start() {
		return time_start;
	}
	public void setTime_start(String time_start) {
		this.time_start = time_start;
	}
	public String getTime_expire() {
		return time_expire;
	}
	public void setTime_expire(String time_expire) {
		this.time_expire = time_expire;
	}
	public String getSign() {
		return sign;
	}
	public void setSign(String sign) {
		this.sign = sign;
	}
	public String getSign_sp_id() {
		return sign_sp_id;
	}
	public void setSign_sp_id(String sign_sp_id) {
		this.sign_sp_id = sign_sp_id;
	}
	
	private Map<String, String> getTextParams() {		
		Map<String, String> txtParams = new HashMap<String, String>();
		txtParams.put("ver", this.ver);
		txtParams.put("charset", this.charset);
		txtParams.put("bank_type", this.bank_type);
		txtParams.put("desc", this.desc);
		if (!StringUtils.isEmpty(this.purchaser_id)) {
			txtParams.put("purchaser_id", this.purchaser_id);
		}
		txtParams.put("pay_channel", this.pay_channel);
		txtParams.put("bargainor_id", this.bargainor_id);
		txtParams.put("sp_billno", this.sp_billno);
		txtParams.put("total_fee", String.valueOf(this.total_fee));
		txtParams.put("fee_type", String.valueOf(this.fee_type));
		txtParams.put("notify_url", this.notify_url);
		if (!StringUtils.isEmpty(this.attach)) {
			txtParams.put("attach", this.attach);
		}
		if (!StringUtils.isEmpty(this.time_start)) {
			txtParams.put("time_start", this.time_start);
		}
		if (!StringUtils.isEmpty(this.time_expire)) {
			txtParams.put("time_expire", this.time_expire);
		}
		if (!StringUtils.isEmpty(this.sign_sp_id)) {
			txtParams.put("sign_sp_id", this.sign_sp_id);		
		}
		return txtParams;
	}
	
	public void toSign(String payKey){
		Map<String, String> map = getTextParams();
		SortedSet<String> keys = new TreeSet<String>(map.keySet());
		Iterator<String> it = keys.iterator();
		StringBuffer signStr = new StringBuffer();
		while (it.hasNext()) {
			String name = it.next();
			String value = map.get(name);
			signStr.append(name).append("=").append(value);
			if (it.hasNext()) {
				signStr.append("&");
			}
		}
		signStr.append("&key=");
		signStr.append(payKey);
		MessageDigest md = null;
		byte[] array = null;
		try {
			md = MessageDigest.getInstance("MD5");
			array = md.digest(signStr.toString().getBytes("UTF-8"));
		} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
			e.printStackTrace();
			return;
		}
		String signTmp = new String(Hex.encodeHex(array));
		this.sign = signTmp.toUpperCase(); 
	}
	
	/**
	 * 包含签名的xml字串，用于微信支付接口请求
	 * 
	 * @return
	 */
	public String getXML() {
		Map<String, String> map = getTextParams();
		map.put("sign", this.sign);
		String xmlData = XMLUtil.MapToXml(map);
		return xmlData;
	}

	/**
	 * 包含签名的querystring字串，用于QQ支付接口请求
	 * 
	 * @return
	 */
	public String getParams() {
		Map<String, String> map = getTextParams();		
		Set<Entry<String, String>> it = map.entrySet();
		StringBuffer signStr = new StringBuffer();
		for (Entry<String, String> item : it) {
			String name = item.getKey();
			String value = null;
			try {
				value = URLEncoder.encode(item.getValue(), "UTF-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			signStr.append(name).append("=").append(value).append("&");
		}
		signStr.append("sign").append("=").append(this.sign);
		return signStr.toString();
	}
}
